package scm.rawMaterialPurchasing.managedbean;

import crm.clientManagement.entity.AddressEntity;
import java.io.IOException;
import java.io.Serializable;

import java.util.ArrayList;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import scm.rawMaterialPurchasing.entity.SupplierEntity;
import scm.rawMaterialPurchasing.session.SupplierSessionBean;
import javax.faces.event.ActionEvent;
import java.util.List;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.event.FlowEvent;
import org.primefaces.model.DualListModel;
import scm.rawMaterialPurchasing.entity.RawMaterialEntity;
import scm.rawMaterialPurchasing.entity.SupplierLineItemEntity;

@ManagedBean
@ViewScoped
public class CreateNewSupplierManagedBean implements Serializable {

    @EJB
    SupplierSessionBean supplierSessionBean;
    private SupplierEntity supplier;
    private AddressEntity address;
    private DualListModel rawMaterialDual;
    private List<String> rawMaterialNameList;
    private List<RawMaterialEntity> target = new ArrayList<RawMaterialEntity>();
  
    private List<Double> pricing;
    private List<RawMaterialEntity> source;
    private List<SupplierLineItemEntity> supplierLineItemsList;
//    private String privateTitle;
//    private String publicTitle;
    private final static Logger logger = Logger.getLogger(CreateNewSupplierManagedBean.class.getName());

    public CreateNewSupplierManagedBean() {

        pricing = new ArrayList<Double>();
        supplierSessionBean = new SupplierSessionBean();
        supplier = new SupplierEntity();
        rawMaterialDual = new DualListModel();
        address = new AddressEntity();
      
    }

    public DualListModel<RawMaterialEntity> getRawMaterialDual() {
        return rawMaterialDual;
    }

    public void setRawMaterialDual(DualListModel<RawMaterialEntity> rawMaterialDual) {
        this.rawMaterialDual = rawMaterialDual;

    }

    public void dualListFunction() {
        source = supplierSessionBean.getAllRawMaterial();
        rawMaterialDual = new DualListModel(source, target);
         System.out.println("SDFSDFSDFSDf 112  "+ rawMaterialDual.getTarget().size());
    }

    public void dualListComplete() {
          supplierLineItemsList=new ArrayList<SupplierLineItemEntity>();
        System.out.println("SDFSDFSDFSDf   "+ rawMaterialDual.getTarget().size());
        for (int i = 0; i < rawMaterialDual.getTarget().size(); i++) {
            SupplierLineItemEntity temp = new SupplierLineItemEntity();
            temp.setRawMaterial((RawMaterialEntity) rawMaterialDual.getTarget().get(i));
            temp.setPrice(0.0);
            supplierLineItemsList.add(temp);
            
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Completed. Please proceed", ""));
    }

    public String onFlowProcess(FlowEvent event) {
        logger.info("Current wizard step:" + event.getOldStep());
        logger.info("Next step:" + event.getNewStep());

//        if(event.getOldStep().equals("supplierDetail") && event.getNewStep().equals("rawMaterialDetail"))
//        {
//            System.out.println("publicTitle: " + publicTitle);
//            publicTitle=supplier.getContactPersonTitle();
////            if(publicTitle == null)
////            {
////                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select asupplier title.", ""));
////                return event.getOldStep();
////            }
////            else
////            {
////                privateTitle = publicTitle;
////            }
//        }
        
        if(event.getOldStep().equals("supplierDetail") && event.getNewStep().equals("rawMaterialDetail"))
        {
            dualListFunction();
        }

        return event.getNewStep();
    }

    public void save(ActionEvent event) throws IOException {
        supplier.setAddress(address);
        supplierSessionBean.saveSupplier(supplier);
        List<SupplierLineItemEntity> tempList=supplierLineItemsList;
        for(int i=0;i<tempList.size();i++){
            supplierLineItemsList.get(i).setSupplier(supplier);
        }
       supplier.setSupplierLineItems(supplierLineItemsList);
       supplierSessionBean.save(supplierLineItemsList);
       FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Save Successfully", ""));
    }

    public SupplierEntity getSupplier() {
        return supplier;
    }

    public void setSupplier(SupplierEntity supplier) {
        this.supplier = supplier;
    }

    public List<String> getRawMaterialNameList() {
        return rawMaterialNameList;
    }

    public void setRawMaterialNameList(List<String> rawMaterialNameList) {
        this.rawMaterialNameList = rawMaterialNameList;
    }

    public AddressEntity getAddress() {
        return address;
    }

    public void setAddress(AddressEntity address) {
        this.address = address;
    }

    public List<RawMaterialEntity> getTarget() {
        return target;
    }

    public void setTarget(List<RawMaterialEntity> target) {
        this.target = target;
    }

    public List<RawMaterialEntity> getSource() {
        return source;
    }

    public void setSource(List<RawMaterialEntity> source) {
        this.source = source;
    }

    public List<Double> getPricing() {
        return pricing;
    }

    public void setPricing(List<Double> pricing) {
        this.pricing = pricing;
    }

    public List<SupplierLineItemEntity> getSupplierLineItemsList() {
        return supplierLineItemsList;
    }

    public void setSupplierLineItemsList(List<SupplierLineItemEntity> supplierLineItemsList) {
        this.supplierLineItemsList = supplierLineItemsList;
    }
}
